//
//  GestureSettingViewController.swift
//  ezyform
//
//  Created by Moennig Q on 19/7/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
class GestureSettingViewController: ModelBaseViewController,GestureLockProtocol {
    var settingClosure: ((String)->Void)?
    var password: String = ""
    var confirmPassword: String = ""
    var isGuideFlag: Bool = false
    // MARK: - Properties
    let customView = GestureSettingView()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
    }
    func initUI(){
        self.view = self.customView
        self.customView.setTitle(R.string.localizable.gestureTitle())
        self.customView.backToPreBtn.addTarget(self, action: #selector(backToPreBtnClick), for: UIControl.Event.touchUpInside)
        self.customView.gestureView.gestureLockDelegate = self
        if self.isGuideFlag{//如果是引导图，则不显示closeBtn
            self.customView.closeBtn.isHidden = true
            self.customView.closeBtn.isEnabled = false
        }else{//从Me里边进入的才显示
            self.customView.closeBtn.addTarget(self, action: #selector(dismissVC), for: UIControl.Event.touchUpInside)
        }
    }
    @objc private func backToPreBtnClick() {
        self.settingClosure?("")
        self.navigationController?.popViewController(animated: true)
    }
    //GestureLockProtocol
    func gestureLockSuccess(tagArray: [Int]){
        if tagArray.count >= self.customView.gestureView.atLeastPoints {//符合最少的点数
            let resultPwd = LoginMethodServcie().dealTagArrayToStr(tagArray)
            if self.password == ""{//第一步成功
                self.password = resultPwd
                self.customView.miniView.setSelectTagArray(tagArray)
                self.customView.dealErrorGesture(.toConfirmGesture)
            }else {//确认密码
                if self.password == resultPwd{
                    self.navigationController?.popViewController(animated: true)
                    self.settingClosure?(self.password)
                }else{
                    self.customView.dealErrorGesture(.redraw)
                }
            }
        }else{
            if self.password == ""{//第一步，不符合要提醒至少5个
                self.customView.dealErrorGesture(.atLeast5)
            }else{
                self.customView.dealErrorGesture(.redraw)
            }
        }
        self.customView.gestureView.recoverNodeStatus()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        DebugLog("\(self) didReceiveMemoryWarning")
    }
}
